package top.lshaci.learning.schedule;

import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * Task
 *
 * @author lshaci
 * @since 1.0.0
 */
public class ScheduledTest {

    static ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(4);

    @Slf4j
    static
    class Send implements Runnable {
        List<Integer> interval;
        int index;

        public Send(String time) {
            interval = Arrays.stream(time.split(",")).map(Integer::valueOf).collect(Collectors.toList());
        }

        @Override
        public void run() {
            if (index < interval.size()) {
                log.debug("第 {} 次发送消息", index);
                executorService.schedule(this, interval.get(index++), TimeUnit.SECONDS);
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        final Send send = new Send("1,1,2,2,3,4");
        new Thread(send).start();
    }
}
